﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YJKSparseDistance.ModelLayer;
using YJKSparseDistance.Properties;
using YJKSparseDistance.ViewLayer;

namespace YJKSparseDistance
{
	public static class EventUseMethod
	{
		public static Schema CreatSchema(Autodesk.Revit.DB.Document doc, string schemaGuid)
		{
			Guid guid = new Guid(schemaGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				Transaction transaction = new Transaction(doc, "CreateSchema");
				try
				{
					transaction.Start();
					SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
					Type typeFromHandle = typeof(InsertData);
					schemaBuilder.SetSchemaName(Resources.Str_SchemaName);
					foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
					{
						if (fieldInfo.FieldType == typeof(List<string>))
						{
							schemaBuilder.AddArrayField(fieldInfo.Name, typeof(string));
						}
					}
					schema = schemaBuilder.Finish();
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
			return schema;
		}

		public static void LocationAWay(UIDocument useUIDoc, ResultBean useBean, ResultForm useForm)
		{
			Document document = useUIDoc.Document;
			ViewPlan viewPlan = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			Level level = useBean.EndPoint.ThisRoom.Level;
			List<int> list = new List<int>();
			ElementSet elementSet = new ElementSet();
			List<Element> list2 = new List<Element>();
			List<ElementId> lstElmenent = new List<ElementId>();
			Transaction transaction = EventUseMethod.DeleteLines(useForm, document, lstElmenent);
			foreach (Element element in filteredElementCollector.OfClass(typeof(ViewPlan)))
			{
				ViewPlan viewPlan2 = (ViewPlan)element;
				if (viewPlan2 != null && viewPlan2.GenLevel != null && viewPlan2.GenLevel.Id.IntegerValue == level.Id.IntegerValue)
				{
					viewPlan = viewPlan2;
					break;
				}
			}
			transaction = new Transaction(document, "LocationWay");
			try
			{
				transaction.Start();
				if (viewPlan != null)
				{
					foreach (Line line in useBean.GetLines())
					{
						DetailCurve detailCurve = document.Create.NewDetailCurve(viewPlan, line);
						list.Add(detailCurve.Id.IntegerValue);
						elementSet.Insert(detailCurve);
						list2.Add(detailCurve);
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
				list.Clear();
				elementSet.Clear();
				list2.Clear();
			}
			if (0 < list2.Count)
			{
				useUIDoc.ShowElements(elementSet);
				RevitVersionFuncs.SetSelection(useUIDoc.Selection, list2);
				useUIDoc.RefreshActiveView();
			}
			useForm.LstElementIdsNeedDelete.AddRange(list);
		}

		public static void AddValue(string inputGuid, UIDocument useUIDoc, ResultForm useForm)
		{
			Document document = useUIDoc.Document;
			List<ElementId> lstElmenent = new List<ElementId>();
			EventUseMethod.DeleteLines(useForm, document, lstElmenent);
			Area area = null;
			for (int i = 0; i < useForm.LstNeedTrust.Keys.Count; i++)
			{
				area = useForm.LstNeedTrust.Keys.ElementAt(i);
				InsertData insertData = new InsertData();
				foreach (ResultBean resultBean in useForm.LstNeedTrust[area])
				{
					insertData.m_lstInsetData.Add(resultBean.ToString());
				}
				InsetDataUtility.TryWriteToElement(inputGuid, area, insertData);
			}
		}

		private static Transaction DeleteLines(ResultForm useForm, Autodesk.Revit.DB.Document useDoc, List<ElementId> lstElmenent)
		{
			Transaction transaction = new Transaction(useDoc, "deletLine");
			foreach (int num in useForm.LstElementIdsNeedDelete)
			{
				if (useDoc.GetElement(new ElementId(num)) != null)
				{
					lstElmenent.Add(new ElementId(num));
				}
			}
			useForm.LstElementIdsNeedDelete.Clear();
			try
			{
				transaction.Start();
				useDoc.Delete(lstElmenent);
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			return transaction;
		}
	}
}
